
import Api from "@/api/api-declare";
import PD from "../libs";
import type { ApiCommonTypes } from "@/api/api-types/api-common-types";
import Store from "@/store";

export function useLicense() {
	const license = ref<ApiCommonTypes.LicenseItem[]>([]); //列表
	
	const getLicense = () : Promise<void> => {
		return new Promise((r) => {
			PD.UniHelp.loading();
			Api.ApiCommon.getLicenseList().then(res => {
				license.value = res.items;
				r();
			}).finally(() => PD.UniHelp.loading(false));
		});
	};

	const agreeLicense = (licenseIds : number[]) : Promise<void> => {
		return new Promise((r) => {
			PD.UniHelp.loading();
			Api.ApiCommon.agreeLicense({
				license_id : licenseIds,
				user_id : Store.user().openid
			}).then(() => {
				r();
			}).finally(() => PD.UniHelp.loading(false));
		});
	};

	const licenseDetail = ref<ApiCommonTypes.LicenseItem>();//详情
	const getLicenseDetail = (licenseId : number) : Promise<void> => {
		return new Promise((r) => {
			PD.UniHelp.loading();
			Api.ApiCommon.getLicenseDetail({
				id : licenseId
			}).then(res => {
				licenseDetail.value = res;
				r();
			}).finally(() => PD.UniHelp.loading(false));
		});
	};

	return {
		license,
		licenseDetail,
		getLicense,
		agreeLicense,
		getLicenseDetail
	};
}